// pages/myFile/myFile.js
const db = wx.cloud.database();
const fileInfo = db.collection('File-Info');
Page({
  /* 页面的初始数据*/
  data: {
    fileCollection: [],
    searchVal: null,
    total: 0,
    state:2,
    deleteList:[]
    
  },

/*删除功能*/
  handle1:function(){
     this.setData({
     state:1
   })
  },
  handle2:function(){
    this.setData({
    state:2
  })
 },
  onCheckboxChange:function(e){
    this.setData({
      deleteList:e.detail.value
    })
    if(this.data.deleteList.length>0)
    {
      this.setData({
        state:0
      })
    }
    else
    this.setData({
      state:1
    })
  },
  handleDelete:function(){
    const that = this;
    const docId =this.data.deleteList;
    wx.showLoading({ title: '删除中...' });
    for(const docId of docId){
    const db = wx.cloud.database();
    db.collection('File-Info')
      .doc(docId)
      .remove({
        success: res => {
          wx.hideLoading();
          wx.showToast({ title: '删除成功', icon: 'success' });
          // 从列表中移除已删除的项
          const newList = this.data.fileCollection.filter(item => item._id !== docId);
          this.setData({
            fileCollection: newList
          
          });
        },
        fail: err => {
          wx.hideLoading();
          console.error('删除失败', err);
          wx.showToast({ title: '删除失败', icon:'error' });
        }
      });
      }
      wx.showToast({ title: '删除成功', icon:'success' });
      this.setData({
        deleteList:[],
        state:2
      })
  },

  /*初始化文件大小的单位*/ 
/*更新文件大小的单位*/
setFileSize: function() {
  var fileCollection=this.data.fileCollection
for(var i=0;i<fileCollection.length;i++){
  var unit = 'KB';
  if (fileCollection[i].fileSize >= 1024) {
    fileCollection[i].fileSize /= 1024;
    unit = 'MB';   
    if (fileCollection[i].fileSize >= 1024) {
      fileCollection[i].fileSize /= 1024;
      unit = 'GB';    
    }
  }
  // 保留两位小数
  fileCollection[i].fileSize = Math.round(fileCollection[i].fileSize * 100) / 100;
  fileCollection[i]={
    ...fileCollection[i],
    fileSizeText: `${fileCollection[i].fileSize} ${unit}`  // 新增格式化后的文件大小文本
  };
}
console.log(fileCollection)
this.setData({
  fileCollection:fileCollection
});
},



  /* 生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    getApp().globalData.isMyFile = true

    //获取集合总数
    fileInfo.where({
      _openid: getApp().globalData.userInfo.openid
    }).count({
      success: res => {
        that.setData({
          total: res.total
        })
      }
    })

    //在云数据库fileInfo中根据用户openid查找文件信息集合
    fileInfo.where({
      _openid: getApp().globalData.userInfo.openid
    }).orderBy('fileTime', 'desc').get({
      success: res => {
        //console.log(res)
        that.setData({
          fileCollection: res.data
        })
      },
      fail: err => {
        console.error(err)
      }
    })

  },

  onShow:function(){
    getApp().globalData.isMyFile = true
    setTimeout(()=>{
      this.setFileSize()
    },1000) 
  },

  //关键字搜索
  input: function (e) {
    this.setData({
      searchVal: e.detail.value
    })
  },

  search: function () {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })

    //获取集合总数
    fileInfo.where({
      _openid: getApp().globalData.userInfo.openid,
      fileName: db.RegExp({
        regexp: that.data.searchVal,
        options: 'i'
      })
    }).count({
      success: res => {
        that.setData({
          total: res.total
        })
      }
    })


    fileInfo.where({
      _openid: getApp().globalData.userInfo.openid,
      fileName: db.RegExp({
        regexp: that.data.searchVal,
        options: 'i'
      })
    }).orderBy('fileTime', 'desc').get({
      success: res => {
        wx.hideLoading()
        that.setData({
          fileCollection: res.data
        })
        that.pageData.skip = 20
        //console.log(res, that.data.searchVal)
      }
    })
  },

  //下拉刷新
  onReachBottom: function () {
    var that = this;
    wx.showLoading({
      title: '数据加载中',
    })
    if (that.data.searchVal) { //用户搜索结果下拉刷新
      fileInfo.where({
        _openid: getApp().globalData.userInfo.openid,
        fileName: db.RegExp({
          regexp: that.data.searchVal,
          options: 'i'
        })
      }).skip(that.pageData.skip).orderBy('fileTime', 'desc').get({
        success: res => {
          //console.log(res)
          wx.hideLoading()
          let oldData = that.data.fileCollection
          that.setData({
            fileCollection: oldData.concat(res.data)
          })
          that.pageData.skip = that.pageData.skip + 20;
        },
        fail: err => {
          console.error(err)
        }
      })
    } else { //所有资源下拉刷新
      fileInfo.where({
        _openid: getApp().globalData.userInfo.openid
      }).skip(that.pageData.skip).orderBy('fileTime', 'desc').get({
        success: res => {
          //console.log(res)
          wx.hideLoading()
          let oldData = that.data.fileCollection
          that.setData({
            fileCollection: oldData.concat(res.data)
          })
          that.pageData.skip = that.pageData.skip + 20;
        },
        fail: err => {
          console.error(err)
        }
      })

    }

  },
  //存放触底刷新跳过的页面数
  pageData: {
    skip: 20
  }

})